<template>
    <div class="table">
        <!--主体-->
        <div class="container">
            <i class="el-icon-refresh" style="font-size: 18px;padding-bottom: 20px" @click="update"></i>
            <!--搜索-->
            <div class="handle-box">
                <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"
                          @keyup.enter.native="search">
                    <i slot="prefix" class="el-input__icon el-icon-search"></i>
                </el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
                <el-button type="success" size="small" @click="handleAdd">新增</el-button>
            </div>
            <!--表格-->
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column label="医生ID" prop="uuid"></el-table-column>
                <el-table-column label="医生职称" prop="position"></el-table-column>
                <el-table-column label="医生名字" prop="title"></el-table-column>
                <el-table-column label="医生所在医院" prop="hospital"></el-table-column>
                <el-table-column label="操作" width="230">
                    <template slot-scope="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="40%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="医生id">
                    <el-input v-model="form.uuid" disabled></el-input>
                </el-form-item>
                <el-form-item label="医生名字">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="中文名字">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="个人专长">
                    <el-input v-model="form.special"></el-input>
                </el-form-item>
                <el-form-item label="医生所在医院">
                    <el-input v-model="form.hospital"></el-input>
                </el-form-item>
                <el-form-item label="医院id">
                    <el-input v-model="form.hospitalId"></el-input>
                </el-form-item>
                <el-form-item label="医院等级">
                    <el-input v-model="form.hco_level"></el-input>
                </el-form-item>
                <el-form-item label="科室id">
                    <el-input v-model="form.parent_dpt_id"></el-input>
                </el-form-item>
                <el-form-item label="医生职称">
                    <el-input v-model="form.position"></el-input>
                </el-form-item>
                <el-form-item label="清洗后职称">
                    <el-input v-model="form.position_cleaned"></el-input>
                </el-form-item>
                <el-form-item label="是否激活">
                    <el-input v-model="form.is_active"></el-input>
                </el-form-item>
                <el-form-item label="国家">
                    <el-input v-model="form.national"></el-input>
                </el-form-item>
                <el-form-item label="电话">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="省">
                    <el-input v-model="form.province"></el-input>
                </el-form-item>
                <el-form-item label="sid">
                    <el-input v-model="form.sid"></el-input>
                </el-form-item>
                <el-form-item label="记录id">
                    <el-input v-model="form.sortId"></el-input>
                </el-form-item>
                <el-form-item label="发布日期">
                    <el-input v-model="form.publish_date"></el-input>
                </el-form-item>
                <el-form-item label="更新时间">
                    <el-input v-model="form.updateTime"></el-input>
                </el-form-item>
                <el-form-item label="来源">
                    <el-input v-model="form.source"></el-input>
                </el-form-item>
                <el-form-item label="网址">
                    <el-input v-model="form.url"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增提示框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="40%" center>
            <div class="demo-input-suffix">
                <div>
                    <span class="spn">医生名字:</span>
                    <el-input v-model="form1.title" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must1">*必填项</span>
                </div>
                <div>
                    <span class="spn">中文名字:</span>
                    <el-input v-model="form1.name" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must2">*必填项</span>
                </div>
                <div>
                    <span class="spn">医院等级:</span>
                    <el-input v-model="form1.hco_level" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must3">*必填项</span>
                </div>
                <div>
                    <span class="spn">医生所在医院:</span>
                    <el-input v-model="form1.hospital" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must4">*必填项</span>
                </div>
                <div>
                    <span class="spn">科室:</span>
                    <el-input v-model="form1.dept" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must5">*必填项</span>
                </div>
                <div>
                    <span class="spn">父科室:</span>
                    <el-input v-model="form1.parent_dpt" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must6">*必填项</span>
                </div>
                <div>
                    <span class="spn">行政职务:</span>
                    <el-input v-model="form1.executive_Title" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must7">*必填项</span>
                </div>
                <div>
                    <span class="spn">医生职称:</span>
                    <el-input v-model="form1.position" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must8">*必填项</span>
                </div>
                <div>
                    <span class="spn">个人经验:</span>
                    <el-input v-model="form1.experience" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must9">*必填项</span>
                </div>
                <div>
                    <span class="spn">个人专长:</span>
                    <el-input v-model="form1.special" placeholder="请输入内容" class="ipt"></el-input>
                    <span style="padding-left: 10px;color: red" v-show="must10">*必填项</span>
                </div>
                <div>
                    <span class="spn">性别:</span>
                    <el-input v-model="form1.gender" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">英文名字:</span>
                    <el-input v-model="form1.en_title" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">生日年份:</span>
                    <el-input v-model="form1.birth_year" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">生日月份:</span>
                    <el-input v-model="form1.birth_month" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">省:</span>
                    <el-input v-model="form1.province" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">市:</span>
                    <el-input v-model="form1.city" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">邮件:</span>
                    <el-input v-model="form1.email" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">国家:</span>
                    <el-input v-model="form1.national" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">电话:</span>
                    <el-input v-model="form1.phone" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">来源:</span>
                    <el-input v-model="form1.source" placeholder="请输入内容" class="ipt"></el-input>
                </div>
                <div>
                    <span class="spn">网址:</span>
                    <el-input v-model="form1.url" placeholder="请输入内容" class="ipt"></el-input>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="adder">确 定</el-button>

            </span>
        </el-dialog>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                /*医生显示接口*/
                showUrl: '',
                /*医生搜索接口*/
/*                searchUrl: 'http://192.168.1.55:80/hcpmdm/hcp_query',*/
                searchUrl: 'http://bigdata-api.huimeimt.cn/resource_server/hcpmdm/hcp_query',
                /*医生增加接口*/
/*                adderUrl: 'http://192.168.1.55:80/hcpmdm/hcp_insert',*/
                adderUrl: 'http://bigdata-api.huimeimt.cn/resource_server/hcpmdm/hcp_insert',
                /*医生删除接口*/
/*                deleteRowUrl: 'http://192.168.1.55:80/hcpmdm/hcp_del/',*/
                deleteRowUrl: 'http://bigdata-api.huimeimt.cn/resource_server/hcpmdm/hcp_del/',
                /*医生编辑接口*/
                /*                saveEditUrl: 'http://192.168.1.55:80/hcpmdm/hcp_update',*/
                saveEditUrl: 'http://bigdata-api.huimeimt.cn/resource_server/hcpmdm/hcp_update',

/*                associatedUrl:'http://192.168.1.55:80/hcohcphandle/hcohcp_relations',*/
                associatedUrl:'http://bigdata-api.huimeimt.cn/resource_server/hcohcphandle/hcohcp_relations',

                access_token: '',
                tableData: [],
                /*编辑*/
                editVisible: false,
                /*新增*/
                addVisible: false,
                /*删除*/
                delVisible: false,
                /*搜索*/
                select_word: '',
                is_search: false,
                /*数据*/
                form: {
                    title: '',
                    parent_dpt_id: '',
                    hco_level: '',
                    hospital: '',
                    birth_month: '',
                    birth_year: '',
                    url: '',
                    experience: '',
                    city: '',
                    createTime: '',
                    dept: '',
                    dpt_cleaned: '',
                    email: '',
                    en_title: '',
                    executive_Title: '',
                    gender: '',
                    hospitalId: '',
                    is_active: '',
                    name: '',
                    national: '',
                    parent_dpt: '',
                    phone: '',
                    position: '',
                    position_cleaned: '',
                    province: '',
                    publish_date: '',
                    sid: '',
                    sortId: '',
                    source: '',
                    special: '',
                    updateTime: '',
                    uuid: '',
                },
                /*新增数据*/
                form1: {
                    title: '',
                    parent_dpt_id: '',
                    hco_level: '',
                    hospital: '',
                    birth_month: '',
                    birth_year: '',
                    url: '',
                    experience: '',
                    city: '',
                    createTime: '',
                    dept: '',
                    dpt_cleaned: '',
                    email: '',
                    en_title: '',
                    executive_Title: '',
                    gender: '',
                    hospitalId: '',
                    is_active: '',
                    name: '',
                    national: '中国',
                    parent_dpt: '',
                    phone: '',
                    position: '',
                    position_cleaned: '',
                    province: '',
                    publish_date: '',
                    sid: '',
                    sortId: '',
                    source: '',
                    special: '',
                    updateTime: '',
                    uuid: '',
                },
                idx: -1,
                must1:false,
                must2:false,
                must3:false,
                must4:false,
                must5:false,
                must6:false,
                must7:false,
                must8:false,
                must9:false,
                must10:false,
            }
        },
        created() {
            this.gainTtoken();
        },
        methods: {
            /*获取token*/
            gainTtoken() {
                this.$axios({
                    method: 'GET',
                    url: 'http://bigdata-oauth.huimeimt.cn/Dataplatform/token',
                }).then((res) => {
                    this.access_token = res.data.access_token
                })
            },
            /*编辑处理*/
            handleEdit(index, row) {
                this.idx = index;
                const item = this.tableData[index];
                this.form = {
                    title: item.title,
                    parent_dpt_id: item.parent_dpt_id,
                    hco_level: item.hco_level,
                    hospital: item.hospital,
                    birth_month: item.birth_month,
                    birth_year: item.birth_year,
                    url: item.url,
                    experience: item.experience,
                    city: item.city,
                    createTime: item.createTime,
                    dept: item.dept,
                    dpt_cleaned: item.dpt_cleaned,
                    email: item.email,
                    en_title: item.en_title,
                    executive_Title: item.executive_Title,
                    gender: item.gender,
                    hospitalId: item.hospitalId,
                    is_active: item.is_active,
                    name: item.name,
                    national: item.national,
                    parent_dpt: item.parent_dpt,
                    phone: item.phone,
                    position: item.position,
                    position_cleaned: item.position_cleaned,
                    province: item.province,
                    publish_date: item.publish_date,
                    sid: item.sid,
                    sortId: item.sortId,
                    source: item.source,
                    special: item.special,
                    updateTime: item.updateTime,
                    uuid: item.uuid,
                };
                this.editVisible = true;
            },
            /*保存编辑*/
            saveEdit() {
                this.$axios({
                    method: 'POST',
                    headers:{'Content-Type':'application/json;charset=UTF-8'},
                    data: JSON.stringify(this.form),
                    url: this.saveEditUrl + "?access_token=" + this.access_token,
                }).then((res) => {
                    this.editVisible = false;
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                    location.reload();
                })
            },
            /*新增处理*/
            handleAdd() {
                this.addVisible = true;
            },
            /*确定新增*/
            adder() {
                const timestamp = Date.parse(new Date());
                this.form1.createTime = this.format(timestamp);
                this.form1.updateTime = this.format(timestamp);
                this.examination();
                this.$axios({
                    method: 'POST',
                    headers:{'Content-Type':'application/json;charset=UTF-8'},
                    data: JSON.stringify(this.form1),
                    url: this.adderUrl + "?access_token=" + this.access_token,
                }).then((res) => {
                    this.addVisible = false;
                    this.$message.success(`新增成功`);
/*                    location.reload();*/
                });

            },
            /*删除处理*/
            handleDelete(index, row) {
                console.log(index, row);
                this.idx = index;
                this.delVisible = true;
            },
            /*确定删除*/
            deleteRow() {
                let data = this.tableData.splice(this.idx, 1);
                for (let i in data) {
                    this.$axios.delete(this.deleteRowUrl + data[i].sortId + "?access_token=" + this.access_token)
                        .then((res) => {
                            this.$message.success('删除成功');
                            this.delVisible = false;
                        })
                        .catch((err) => {
                            console.log(err);
                        });
                }
            },
            /*搜索*/
            search() {
                this.is_search = true;
                if (this.select_word.length > 0) {
                    this.is_search = true;
                    let data = {"data": this.excludeSpecial(this.select_word)};
                    this.$axios({
                        method: 'POST',
                        headers: {'Content-Type': 'application/json;charset=UTF-8'},
                        url: this.searchUrl + "?access_token=" + this.access_token,
                        data: JSON.stringify(data),
                    }).then((res) => {
                        this.tableData = res.data.result;
                    }).catch((err) => {
                        console.log(err);
                    });
                }
            },
            /*时间戳转换*/
            add0(m) {
                return m < 10 ? '0' + m : m
            },
            format(timestamp) {
                var time = new Date(timestamp);
                var year = time.getFullYear();
                var month = time.getMonth() + 1;
                var date = time.getDate();
                var hours = time.getHours();
                var minutes = time.getMinutes();
                var seconds = time.getSeconds();
                return year + '-' + this.add0(month) + '-' + this.add0(date) + ' ' + this.add0(hours) + ':' + this.add0(minutes) + ':' + this.add0(seconds);
            },
            /*交验*/
            examination() {
                /*医生名字*/
                if (this.form1.title == "") {
                    this.must1 = true;
                    return false;
                } else {
                    this.must1 = false;
                }
                /*中文名字*/
                if (this.form1.name == "") {
                    this.must2 = true;
                    return false;
                } else {
                    this.must2 = false
                }
                /*医院等级*/
                if (this.form1.hco_level == "") {
                    this.must3 = true;
                    return false;
                } else {
                    this.must3 = false
                }
                /*医生所在医院*/
                if (this.form1.hospital == "") {
                    this.must4 = true;
                    return false;
                } else {
                    this.must4 = false
                }
                /*科室*/
                if (this.form1.dept == "") {
                    this.must5 = true;
                    return false;
                } else {
                    this.must5 = false
                }
                /*父科室*/
                if (this.form1.parent_dpt == "") {
                    this.must6 = true;
                    return false;
                } else {
                    this.must6 = false
                }
                /*行政职务*/
                if (this.form1.executive_Title == "") {
                    this.must7 = true;
                    return false;
                } else {
                    this.must7 = false
                }
                /*医生职称*/
                if (this.form1.position == "") {
                    this.must8 = true;
                    return false;
                } else {
                    this.must8 = false
                }
                /*个人经验*/
                if (this.form1.experience == "") {
                    this.must9 = true;
                    return false;
                } else {
                    this.must9 = false
                }
                /*个人专长*/
                if (this.form1.special == "") {
                    this.must10 = true;
                    return false;
                } else {
                    this.must10 = false
                }
/*
                /!*等级、评级ID*!/
                if (this.form1.hco_grade_level.indexOf('甲等') == 2) {
                    this.form1.grade = '甲级'
                    this.form1.grade_id = '01'
                } else if (this.form1.hco_grade_level.indexOf('乙等') == 2) {
                    this.form1.grade = '乙级'
                    this.form1.grade_id = '02'
                } else if (this.form1.hco_grade_level.indexOf('丙等') == 2) {
                    this.form1.grade = '丙级'
                    this.form1.grade_id = '03'
                } else if (this.form1.hco_grade_level == "不详") {
                    this.form1.grade = '不详'
                    this.form1.grade_id = '00'
                }
                /!*级别、等级ID*!/
                if (this.form1.hco_grade_level.indexOf('三级') == 0) {
                    this.form1.level = '三级'
                    this.form1.level_id = '03'
                } else if (this.form1.hco_grade_level.indexOf('二级') == 0) {
                    this.form1.level = '二级'
                    this.form1.level_id = '02'
                } else if (this.form1.hco_grade_level.indexOf('一级') == 0) {
                    this.form1.level = '一级'
                    this.form1.level_id = '01'
                } else if (this.form1.hco_grade_level == "不详") {
                    this.form1.level = '不详'
                    this.form1.level_id = '00'
                }
                */
                /*性别*/
                if (this.form1.gender == '') {
                    this.form1.gender = '暂无信息'
                }
                /*英文名字*/
                if (this.form1.en_title == '') {
                    this.form1.en_title = '暂无信息'
                }
                /*生日年份*/
                if (this.form1.birth_year == '') {
                    this.form1.birth_year = '暂无信息'
                }
                /*生日月份*/
                if (this.form1.birth_month == '') {
                    this.form1.birth_month = '暂无信息'
                }
                /*省*/
                if (this.form1.province == '') {
                    this.form1.province = '暂无信息'
                }
                /*市*/
                if (this.form1.city == '') {
                    this.form1.city = '暂无信息'
                }
                /*邮件*/
                if (this.form1.email == '') {
                    this.form1.email = '暂无信息'
                }
                /*电话*/
                if (this.form1.phone == '') {
                    this.form1.phone = '暂无信息'
                }
                /*来源*/
                if (this.form1.source == '') {
                    this.form1.source = '暂无信息'
                }
                /*网址*/
                if (this.form1.url == '') {
                    this.form1.url = '暂无信息'
                }
            },
            /*刷新*/
            update(){location.reload();},
            /*去除非法字符*/
            excludeSpecial(s) {s = s.replace(/[^\u4e00-\u9fa5a-zA-Z0-9\w]/g,' ');return s;}
        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .del-dialog-cnt {
        font-size: 16px;
        text-align: center
    }

    .spn {
        display: inline-block;
        width: 20%;
    }

    .ipt {
        padding: 5px 0;
        width: 65%;
    }
</style>
